L14 Tables 1

Data Visualization (STAT 302)

Author

DAVIS JOHNSON

Dataset

We will be using the pizzaplace dataset included in the gt package, the house_prices, stream_data, and team_logo dataset which should be located in your /data subdirectory. Each dataset will require a bit of data wrangling provided in their respective exercise code chunk.

Exercise 1

Use the pizza_summary dataset created below to recreate the following graphic as precisely as possible. We will work through this exercise together to provide a high level overview of a few of the ways you can layer on table elements to make a simple but visually appealing table.

Hints:

  • md(): Interpret input text as Markdown-formatted text
Pizza Place Annual Pizza Sales
Fiscal Year 2015
Number
Sold
Annual
Revenue
classic
big_meat 1,914 $22,968.00
classic_dlx* 2,453 $38,180.50
hawaiian 2,422 $32,273.25
ital_cpcllo 1,438 $25,094.00
napolitana 1,464 $24,087.00
pep_msh_pep 1,359 $18,834.50
pepperoni 2,418 $30,161.75
the_greek 1,420 $28,454.10
chicken
bbq_ckn 2,432 $42,768.00
cali_ckn 2,370 $41,409.50
ckn_alfredo 987 $16,900.25
ckn_pesto 973 $16,701.75
southw_ckn 1,917 $34,705.75
thai_ckn 2,371 $43,434.25
Source: gt package dataset
* Our most popular pizza!

Exercise 2

Using the pizza_size dataset derived from the pizzaplace dataset, recreate the following graphic as precisely as possible.

Table 1

Hints:

  • tab_style() is useful for changing cell colors
  • Hex codes: "#F8766D", "#00BA38", "#619CFF"
  • size L: bold
  • size M: italic
  • size S: "white"
Pizza Place Annual Pizza Sales
Fiscal Year 2015
number_sold profit
chicken
L 4932 102339.00
M 3894 65224.50
S 2224 28356.00
classic
L 4057 74518.50
M 4112 60581.75
S 6139 69870.25
veggie
L 5403 104202.70
M 3583 57101.00
S 2663 32386.75

Table 2

Hints:

  • gt_color_rows is useful for changing entire column colors
  • manual colors c("white", "skyblue")
  • Pastel1 palette from RColorBrewer package
Pizza Place Annual Pizza Sales
Fiscal Year 2015
number_sold profit
chicken
L 4932 102339.00
M 3894 65224.50
S 2224 28356.00
classic
L 4057 74518.50
M 4112 60581.75
S 6139 69870.25
veggie
L 5403 104202.70
M 3583 57101.00
S 2663 32386.75

Exercise 3

Using the house_illinois dataset derived from the house_price dataset, recreate the following graphic as precisely as possible.

Similar to the ggplot scales_*() function, the gt package has useful transformations that can be performed using the following syntax tab_*(), fmt_*(), cols_*(). Check the package documentation for a full list of options.

Average Annual Housing Price Index (HPI)
Illinois Prices for 2006-2010
Housing Price Index
Value % Change
2005 $138.39 7.81%
2006 $145.69 5.31%
2007 $145.54 (0.09%)
1 2008 $135.06 (7.22%)
2009 $122.30 (9.40%)
2010 $116.27 (4.93%)
1 Housing market crash

Hints:

  • color is "red" for all percents less than 0
  • percent is accounting
  • currency "USD"
  • "Housing Price Index" label is a tab_*() layer

Exercise 4

Adding icons and graphics to a table is made easy with gtExtras. Using the stream_data dataset, recreate the following graphic as precisely as possible.

Hints:

  • gt_plt_bullet()
  • gt_fa_column()
  • nytimes theme()
  • fmt_symbol_first()
distributor type ratio nominee
Netflix
WiFi
27.7%
HBO
Video
28.8 
NBC
Television
20.5 
ABC
Television
16.0 
Hulu
WiFi
2.0 
FX Networks
Video
11.1 
Prime Video
WiFi
8.9 
Apple TV+
WiFi
23.8 
CBS
Television
2.5 

Exercise 5

Using the team_logo dataset, recreate the following graphic as precisely as possible.

The tab_options() layer for gt is similar to the theme() option for ggplot. Here you can add customization to the background colors, line type, size, etc.

Hints:

  • gt_img_rows with height = 30
  • tab options:
    • data row padding is px(1)
    • table background color is "gray90"
    • table width is 100%
    • table font color is "red"
    • column labels have background color "black", font size "large", and font weight "bold",
    • table body has hlines color "red"
    • table body has border bottom color "red"
team_name team_nick team_conf
Arizona Cardinals Cardinals NFC
Atlanta Falcons Falcons NFC
Baltimore Ravens Ravens AFC
Buffalo Bills Bills AFC
Carolina Panthers Panthers NFC
Chicago Bears Bears NFC